<?php
require_once "config.php";
require_once "Includes/conexion.class.php";

//include "Includes/SoloPortal_Conexion.php";
require_once "Includes/soloFun_Generales.php";
//include "SoloServices/inc_joincompany.php";

$co_usua = strtolower($_POST["SoloUser"]);
$cl_usua = strtolower($_POST["SoloPass"]);

if(empty($co_usua))
    header("Location: index.php?SoloPageId=1&SoloError=Usuario no existe");

//Valida el Usuario
$ssql = "select a.*,b.ds_perf from solotb_usuario a, solotb_perfil b "
        ." where a.co_perf = b.co_perf "
        ."   and a.co_usua = '$co_usua'";
//echo $ssql;

//re
$oConexion = new Conexion($WORKFLOW["motor_base"], $WORKFLOW["db_usuario"], $WORKFLOW["db_password"], 
        $WORKFLOW["basedatos"], $WORKFLOW["servidor"], $WORKFLOW["flags"], $WORKFLOW["encoding"]);

if($oConexion->CONNECTED){
    //echo date("H:i:s");
        //'response.Write ssql
            //'re
    if(!$oConexion->EjecutarQuery($ssql)){
        //'response.write($_SESSION["SoloConn"))
        //'re
        //echo "Usuario no existe: ".$oConexion->getErrorMessage();
        $oConexion->Cerrar();
        header( "Location: index.php?SoloPageid=1&SoloError=Usuario no existe");
    }else
        $RsLogin = $oConexion->getData();
//printArray($RsLogin);
    $oConexion->liberarResultado();
    $oConexion->Cerrar();
        //'rw cl_usua,trim(rslogin("cl_usua"))
            //'re
            //'if cl_usua<>decript(trim(rslogin("cl_usua"))) then

    //$SoloFlag_ActiveDirectory=SoloFun_GetInfoAdic($co_usua,$co_rolp);
    $SoloFlag_ActiveDirectory = "S";
    $telf = SoloFun_GetInfoAdictelf($co_usua,$co_rolp);

    if ($SoloFlag_ActiveDirectory == "S" && $co_usua != "admin"){
        SoloFun_ConsultarParametros();
        $SoloDomainName = $SoloArrParametros[3][42-1];
        $SoloUserTest = $SoloArrParametros[3][44-1];
        $SoloUserTestPassword = $SoloArrParametros[3][45-1];
        //'rw "1",SoloDomainName
        //'rw "2",SoloUserTest
        //'rw "3",SoloUserTestPassword
        //'re
//        $p1 = $co_usua;
//        $p2 = $cl_usua;
//        $ret = SoloFun_ValidaActiveDirectory($p1, $p2);
//        $ret = 1; //'ojo solo para pruebas QUITAR*****
        //'rw "",ret
        //'re
        //if ($ret == 0)
            //header( "Location: index.php?SoloPageid=1&SoloError=Clave Incorrecta, Contacte al Administrador de Sistemas");
        if ($cl_usua != trim($RsLogin[0]["cl_usua"])){
            header( "Location: index.php?SoloPageid=1&SoloError=Clave Incorrecta");
            return;
        }

    }else{
        if ($cl_usua != trim($RsLogin[0]["cl_usua"])){
            header( "Location: index.php?SoloPageid=1&SoloError=Clave Incorrecta");
            return;
        }
    }
//echo $cl_usua." ".$RsLogin[0]["cl_usua"];
    session_start();
    //'$_SESSION["id_usua")=rslogin("nu_usua")
    //$_SESSION["id_usua"] = $telf;
    $_SESSION["id_usua"] = $RsLogin[0]["nu_usua"];
    //'response.write($_SESSION["id_usua"))
    //'response.End()
    $_SESSION["co_usua"] = $RsLogin[0]["co_usua"];
    $_SESSION["no_usua"] = $RsLogin[0]["no_usua"];
    $_SESSION["co_perf"] = $RsLogin[0]["co_perf"];
    $_SESSION["ds_perf"] = $RsLogin[0]["ds_perf"];
    $_SESSION["empresa"] = $RsLogin[0]["nodo"];
    $_SESSION["login"] = "OK";
    //$_SESSION["sessionid"] = "103224132366567345573";
    $_SESSION["sessionid"] = session_id();
    $Cadena = trim($RsLogin[0]["no_usua"]);
    $SoloArr = preg_split("[\s]", $Cadena);
    $_SESSION["SoloNombre"]= $SoloArr[2];
    //set rslogin=nothing
    //'call SoloFun_JoinCompany
    //'call JoinCompany
    //'response.write($_SESSION["mecodemp"))
    //'response.End()

    header( "Location: soloportal_privada.php?SoloPage=soloportal_Welcome.php");
}else
    header("Location: index.php");
?>